package com.lemon.funplus.cheanh;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

import com.lemon.funplus.CheAnhActivity;
import com.lemon.funplus.R;

public class EraserSettingDialog extends DialogFragment {
	static String eraserID = "eraser_id";
	private View root;
	protected LinearLayout eraserSizeIndicatorContainer;
	protected EraserIndicatorView eraserIndicator;
	protected Paint paint;
	protected SeekBar eraserSize;

	public int radius;
	CheAnhActivity anhActivity;

	public static EraserSettingDialog newInstance(CheAnhActivity anhActivity) {
		EraserSettingDialog f = new EraserSettingDialog(anhActivity);
		return f;
	}

	public EraserSettingDialog(CheAnhActivity anhActivity) {
		this.anhActivity = anhActivity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		radius = anhActivity.brushRadius;
		initViewsSetting();
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog dialog = new AlertDialog.Builder(getActivity()).setTitle("Eraser setting").setView(root).setNegativeButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				anhActivity.eraseRadius = radius;
				Log.e("", "radius: " + radius);
				dismiss();
			}
		}).create();
		return dialog;
	}

	private void initViewsSetting() {
		LayoutInflater inflater = LayoutInflater.from(getActivity());
		root = (ViewGroup) inflater.inflate(R.layout.cheanh_eraser_setting, null, false);
		eraserSizeIndicatorContainer = (LinearLayout) root.findViewById(R.id.eraser_size_indicator_container);

		eraserIndicator = new EraserIndicatorView(this.getActivity(), this);
		eraserSizeIndicatorContainer.addView(eraserIndicator);
		eraserSize = (SeekBar) root.findViewById(R.id.eraser_size);
		eraserSize.setOnSeekBarChangeListener(new EraserSizeSeekbarListener(EraserSettingDialog.this));
	}

	protected void updateSlider() {
		eraserSize.setProgress((int) ((radius - 1) * 100.0f) / (eraserIndicator.maxRadius - 1));
	}

	protected void updateRadius() {
		eraserIndicator.updateRadius();
	}

	protected void setEraseRadius(int rad) {
		radius = rad;
	}

}
